#include "./MessageTest.h"

void ClickZ(HWND hWnd, int x, int y)
{
	::Sleep(100);
	SendMessage(hWnd, WM_LBUTTONDOWN, MK_LBUTTON, MAKELPARAM(x, y));
	SendMessage(hWnd, BM_SETSTATE, 1, 0);
	::Sleep(100);
	SendMessage(hWnd, WM_LBUTTONUP, 0, MAKELPARAM(x, y));
	SendMessage(hWnd, BM_SETSTATE, 0, 0);
}

int Main_02(int _argc, char** _argv)
{
	HWND hWnd = 0;

	const char* pcParentWndCap = "MainWindow";
	const char* pcChildWndCap = "PrintMsg1";

	vector<HWND> vtrParent;
	map<HWND, vector<HWND>> mapChild;
	EnumWnds(
		&vtrParent, (char*)pcParentWndCap, strlen(pcParentWndCap),
		&mapChild, (char*)pcChildWndCap, strlen(pcChildWndCap));

	printf("\n");
	printf("mapChild.size(): %d\n", mapChild.size());
	for (map<HWND, vector<HWND>>::iterator it = mapChild.begin(); it != mapChild.end(); ++it)
	{
		printf("it->first: %d\n", it->first);
	}

	printf("\n");
	printf("Parent window count: %d\n", vtrParent.size());
	for (int i = 0; i < vtrParent.size(); i++)
	{
		HWND hWndParent = vtrParent.at(i);

		if (hWnd == 0)
			hWnd = hWndParent;

		char buf[256] = { 0 };
		int iLen = GetWindowTextA(vtrParent.at(i), buf, sizeof(buf));
		printf("  (%d) %s (%d)(0x%x)\n", i, buf, hWndParent, hWndParent);

		if (mapChild.find(hWndParent) == mapChild.end())
			printf("  has no child window\n");
		else
		{
			vector<HWND> vtr = mapChild.at(hWndParent);
			printf("\t Child window count: %d\n", vtr.size());
			for (int j = 0; j < vtr.size(); j++)
			{
				char buf1[256] = { 0 };
				int iLen = GetWindowTextA(vtr.at(i), buf1, sizeof(buf1));
				printf("\t   [%d.%d] %s (%d)(0x%x)\n", i, j, buf1, vtr.at(i), vtr.at(i));
			}
		}

		printf("\n");
	}


	// *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***

	printf("\n hWnd : %d(0x%X)\n", hWnd, hWnd);
	if (hWnd != 0)
	{
		int x = 20;
		int y = 30;

		HDC dc;
		dc = ::GetWindowDC(hWnd);
		SetPixel(dc, x, y, RGB(0, 0, 0));
		SetPixel(dc, x + 1, y, RGB(0, 0, 0));
		SetPixel(dc, x + 1, y + 1, RGB(0, 0, 0));
		SetPixel(dc, x, y + 1, RGB(0, 0, 0));

		for (int i = 0; i < 1; i++)
			ClickZ(hWnd, x, y);

		////PostMessage(g_hwndEnum, WM_LBUTTONDOWN, 0, x + y * 65536);
		////PostMessage(g_hwndEnum, WM_LBUTTONUP, 0, x + y * 65536);
		//SendMessage(hWnd, WM_LBUTTONDOWN, MK_LBUTTON, MAKELPARAM(x, y));
		////SendMessage(g_hwndEnum, BM_SETSTATE, 1, 0);
		//::Sleep(100);
		//SendMessage(hWnd, WM_LBUTTONUP, 0, MAKELPARAM(x, y));
		////SendMessage(g_hwndEnum, BM_SETSTATE, 0, 0);
	}
	return 0;
}

int Main_WinMsg(int _argc, char** _argv)
{
	return Main_02(_argc, _argv);
}